//
//  VisitorLoginView.swift
//  weiboxiangmu
//
//  Created by wei_jingyun on 15/6/24.
//  Copyright © 2015年 wei_jingyun. All rights reserved.
//

import UIKit

///  代理
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    func visitorRegisterButtonClicked()
    func visitorLoginButtonClicked()
    optional func jjj ()
}



///  访客视图
class VisitorLoginView: UIView {
    weak var myDelegate : VisitorLoginViewDelegate?
    
    func showVisitorView(homeName : String , message: String, isHome: Bool){
        if isHome {
            iconView.hidden = false
        }else {
            iconView.hidden = true
            sendSubviewToBack(maskIconView)
        }
        homeiconView.image = UIImage(named: homeName)
        messageLabel.text = message
    }
    
     func startAn(){
        let  anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        iconView.layer .addAnimation(anim, forKey: nil)
    }
    
    func registerClick(){
        Debug.Log("registerClick")
        myDelegate?.visitorRegisterButtonClicked()
    }
    
    func loginClick(){
        Debug.Log("loginClick")
        myDelegate?.visitorLoginButtonClicked()
    }
    
    
    // 视图的默认构造方法
    // 创建控件，并且指定布局，用 xib & sb 开发不会调用
    override init(frame: CGRect) {
        super.init(frame: frame)
        showView()
    }
    
    // 解档，用 xib & sb 开发会调用
    required init(coder aDecoder: NSCoder) {
        Debug.Log(__FUNCTION__)
        fatalError("init(coder:) has not been implemented")
    }
    
    private func showView (){
        // 背景颜色 237.0 / 255.0 `237` 
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeiconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60  ))
        
        homeiconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeiconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: homeiconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60 ))
        
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20  ))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(240)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : messageLabel]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : messageLabel]))
        
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : registerButton]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : registerButton]))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 20))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(100)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : loginButton]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(40)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : loginButton]))
        
    
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(-16)-[view]-(-16)-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : maskIconView]))
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[view]-160-|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view" : maskIconView]))
        
        
    }

    /// 旋转图标
    lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        return UIImageView(image: image)
    }()
    
    /// 旋转图标
    lazy var maskIconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
        }()
    
    /// 房子
    lazy var homeiconView : UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        return UIImageView(image: image)
    }()
    
    /// 文字标签
    lazy var messageLabel: UILabel = {
        
        // 一旦使用了自动布局，frame 就失效了，在添加控件布局时有效
        let label = UILabel(frame: CGRectMake(0, 0, 240, 40))
        label.text = "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = NSTextAlignment.Center
        // 设置换行宽度
        label.preferredMaxLayoutWidth = 224
        // 最多两行，超出之后...
        label.numberOfLines = 2
        
        label.sizeToFit()
        
        return label
        }()
    
    /// 注册按钮
    lazy var registerButton: UIButton = {
        
        let btn = UIButton(frame: CGRectMake(0, 300 , 100, 40))
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        // 需要对图片切片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    
    /// 登录按钮
    lazy var loginButton: UIButton = {
        
        let btn = UIButton(frame: CGRectMake(200, 300, 100, 40))
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        }()
    

}
